package com.csu.letaobackend.serviceM.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.csu.letaobackend.bean.Admin;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.AdminDao;
import com.csu.letaobackend.service.AdminService;
import com.csu.letaobackend.serviceM.AdminMService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@MapperScan("com.csu.letaobackend.persistence")
public class AdminMServiceImpl implements AdminMService {

      @Autowired
      private AdminDao adminDao;


      @Override
      public CommonResponse<Admin> getAdmin(String adminname) {
            Admin admin = adminDao.selectById(adminname);

            if(admin==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            return CommonResponse.createForSuccess(admin);
      }


      @Override
      public CommonResponse<Admin> getAdmin(String adminname, String password) {
            QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("adminname",adminname);
            queryWrapper.eq("password",password);

            Admin admin = adminDao.selectOne(queryWrapper);
            if(admin==null){
                  return CommonResponse.createForError("用户名或密码错误");
            }
            return CommonResponse.createForSuccess("登录成功",admin);
      }

      @Override
      public CommonResponse<Admin> getAdminByPhone(String memPhone) {
            QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("phone",memPhone);

            Admin admin = adminDao.selectOne(queryWrapper);
            if(admin==null){
                  return CommonResponse.createForError("该手机号对应账户不存在");
            }
            return CommonResponse.createForSuccess("登录成功",admin);
      }

      @Override //测试通过
      public CommonResponse<Admin> updateAdminPassword(String adminname, String newPassword) {
            Admin admin = adminDao.selectById(adminname);
            if(admin==null)
                  return CommonResponse.createForError("用户名不存在");
            else {
                  UpdateWrapper<Admin> updateWrapper =new UpdateWrapper<>();
                  updateWrapper.eq("adminname",adminname);
                  updateWrapper.set("password",newPassword);

                  if(adminDao.update(null,updateWrapper)!=0)
                        return CommonResponse.createForSuccessMessage("修改成功");
                  else
                        return  CommonResponse.createForError("修改失败");
            }
      }

      @Override
      public CommonResponse<Admin> updateAdmin(Admin admin) {
            Admin user1 = adminDao.selectById(admin.getAdminname());

            if(user1==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            else{
                  adminDao.updateById(admin);
            }
            return CommonResponse.createForSuccess("更新成功",admin);
      }

      @Override
      public CommonResponse<Admin> registerAdmin(Admin admin) {
            String adminname = admin.getAdminname();

            QueryWrapper<Admin> queryWrapper= new  QueryWrapper<>();
            queryWrapper.eq("phone",admin.getPhone());

            if (adminDao.selectById(adminname)!=null){
                  return CommonResponse.createForError("用户名已存在");
            }else if(adminDao.selectOne(queryWrapper)!=null) {
                  return CommonResponse.createForError("该手机号已注册");
            }
            else {
                  adminDao.insert(admin);
            }
            return CommonResponse.createForSuccess("注册成功",admin);
      }


      @Override
      public CommonResponse<Admin> deleteAdmin(String adminname) {
            Admin user1 = adminDao.selectById(adminname);

            if(user1==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            else{
                  adminDao.deleteById(adminname);
            }
            return CommonResponse.createForSuccessMessage("删除成功");
      }
}
